<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" %>
<%@include file="/quick/include/common.jsp"%>
<%@include file="/quick/include/commonQuery.jsp"%>
<c:set var="buttons">
    <input type="button" style="margin-right: 5px"   onclick="f_add()" value="<fmt:message key="button.add"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_update()" value="<fmt:message key="button.update"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_delete()" value="<fmt:message key="button.delete"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_cancel()" value="<fmt:message key="button.cancel"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_save()" value="<fmt:message key="button.save"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_query()" value="<fmt:message key="button.query"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_excel()" value="<fmt:message key="button.excel"/>"/>
</c:set>
<div id='toolbar' style="display:none">
<c:out value="${buttons}" escapeXml="false"/>
</div>

<c:set var="subbuttons">
    <input type="button" style="margin-right: 5px"   onclick="f_add2()" value="<fmt:message key="button.add"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_delete2()" value="<fmt:message key="button.delete"/>"/>
</c:set>
<div id='subtoolbar' style="display:none">
<c:out value="${subbuttons}" escapeXml="false"/>
</div>

  <div id="add_win" style="height:300px;display:none"></div>

<script>
var addWin,addForm,addGrid;
Ext.onReady(globalScope._ready);

Ext.onReady(function(){
   var ds=new Ext.data.SimpleStore({
    fields:['sno','sname','grade','department','major','departmentName','majorName'],
    data:[]
   });
   var sm=new Ext.grid.CheckboxSelectionModel();
   var cm = new Ext.grid.ColumnModel([
        new Ext.grid.RowNumberer(), 
        sm,
        {header:'学号',dataIndex:'sno',sortable:true},
        {header:'姓名',dataIndex:'sname',sortable:true},
        {header:'年级',dataIndex:'grade',sortable:true},
        {header:'系',dataIndex:'departmentName',sortable:true},
        {header:'专业',dataIndex:'majorName',sortable:true}
    ]);  
    
    var toolbar=new Ext.ux.toolbar('subToolbar',{});
    addGrid = new Ext.grid.GridPanel({
        tbar:toolbar,
	    ds: ds,
	    height:200,
	    cm: cm,
	    sm:sm
	}); 
	
	var years=Ext.decode("<c:out value='${paramMap.years}' escapeXml='fasle'/>");
	var semesters=Ext.decode("<c:out value='${paramMap.semesters}' escapeXml='fasle'/>");
	var year=Ext.decode("<c:out value='${paramMap.year}' escapeXml='fasle'/>");
	var semester=Ext.decode("<c:out value='${paramMap.semester}' escapeXml='fasle'/>");
	var grades=Ext.decode("<c:out value='${paramMap.gradeJson}' escapeXml='fasle'/>");
	var ycolleges=Ext.decode("<c:out value='${paramMap.yearCollegeJson}' escapeXml='fasle'/>");
	var gmajors=Ext.decode("<c:out value='${paramMap.gradeMajorJson}' escapeXml='fasle'/>");
	
	var addForm=new Ext.form.FormPanel({
        labelAlign: 'right',
        labelWidth:80,
        labelHeight:60,
        frame:true,
        autoWidth:true,
        items:[{
          layout:'column',
          items:[
            {columnWidth:.5,layout:'form',defaultType:'combo',items:[
               _combo('变动学年度','_CYEAR','CYEAR',years,null,year),
               _combo('登记学年度','_RYEAR','RYEAR',years,null,year),
               _combo('转入年级','_GRADE','GRADE',grades),
               _combo('转入专业','_MAJOR','MAJOR',gmajors,['value','text','grade','refid']),
                _combo('立即执行','_IMEXE','IMEXE',[['Y','立即执行'],['N','以后执行']])
              ]  
            },
            {columnWidth:.5,layout:'form',defaultType:'combo',items:[
              _combo('变动学期度','_CSEMESTER','CSEMESTER',semesters,null,semester),
              _combo('登记学期','_RSEMESTER','RSEMESTER',semesters,null,semester),
              _combo('转入系','_DEPARTMENT','DEPARTMENT',ycolleges,['value','text','group']),
              {fieldLabel:'登记日期',xtype:'datefield',anchor :'90%',format : 'Y-m-d',allowBlank:false}]}
          ]
        }]
        
     });   
	
	
	
    Ext.get('add_win').dom.innerHTML="";
	addWin = new Ext.Window({
	    el:'add_win',
	    title: "转专业",
	    width:600,
	    autoScroll:true,
	    modal:true,
	    autoHeight:true,
	    closeAction:'hide',
	    buttonAlign:'center',
	    items: [addGrid,addForm],
	    buttons: [{
	        text:"确定",
	        handler:function(){
	          var r=null;
	          r=grid.getSelectionModel().getSelected();
	          checkForm.getForm().updateRecord(r);
	          
	          var str1="";
	          var smodel=failScoreGrid.getSelectionModel();
	          var failGridStroe=failScoreGrid.getStore();
	          failGridStroe.each(function(rr){
	            if(smodel.isSelected(rr)){
	              str1+=rr.get("COURSE_NO")+",";
	            }
	          });
	          r.set("FAIL_COURSES",str1);
	          if(r.dirty){
	            r.set('CHECK_FLAG','02');
	            r.set('CHECK_ACCOUNT',userName+'('+account+')');
	            var dt=new Date();
	            r.set('CHECK_TIME',dt.format('Y-m-d H:i:s'));
	          }
	          
	          addWin.hide();
	        }
	    },{
	        text:jspVarHandler.fmt_button_close,
	        handler:function(){addWin.hide()}
	    }]
	});
    addWin.render(document.body);
    addWin.hide();	
    
    var gradeCombo=addForm.getForm().findField('_GRADE');
    var yearCollegeCombo=addForm.getForm().findField('_DEPARTMENT');
    var gradeMajorCombo=addForm.getForm().findField('_MAJOR');
    
	 yearCollegeCombo.lastQuery="";
	 gradeMajorCombo.lastQuery="";
	 
	 gradeCombo.on('change',function(combo){
	   yearCollegeCombo.reset();
	   gradeMajorCombo.reset();
	 });
	 yearCollegeCombo.on('change',function(combo){
	   gradeMajorCombo.reset();
	 });
	 
	 yearCollegeCombo.on('focus',function(combo){	
	    var grade=gradeCombo.getValue();
	    yearCollegeCombo.store.filterBy(function(r){
	      if(r.get('group')==grade||r.get('group')=='null') return true;
	    }); 
	 });    
	 
	 gradeMajorCombo.on('focus',function(combo){	
	    var grade=gradeCombo.getValue();
	    var refid=yearCollegeCombo.getValue();
	    gradeMajorCombo.store.filterBy(function(r){
	      if((r.get('grade')==grade&&r.get('refid')==refid)||(r.get('grade')=='null'&&r.get('refid')=='null')) return true; 
	    }); 
	 });    
       
	   
});



function _combo(label,id,hname,data,fields,value){
  if(Ext.isEmpty(fields)) fields=["value","text"];
  return {
                    xtype:'combo',   
                    fieldLabel: label,   
                    store: new Ext.data.SimpleStore({   
                        fields: fields,   
                        data: data   
                    }),   
                    value:value,
                    editable: false,   
                    hiddenName:hname,   
                    id:id,   
                    displayField:'text',   
                    valueField:'value',   
                    typeAhead: true,   
                    mode: 'local',   
                    triggerAction: 'all',   
                    emptyText:'请选择',   
                    selectOnFocus:true,   
                    resizable:true,   
                    allowBlank:false,   
                    forceSelection:true   
  
  }
}

function f_excel(){
  globalScope._exportExcel();
//  globalScope._excel();
}

function f_add(tableName){
  addWin.show()
}

function f_add2(){
  var StudentRecord=Ext.data.Record.create([
    {name:'sno'},{name:'sname'},{name:'grade'},{name:'department'},{name:'major'},{name:'departmentName'},{name:'majorName'}
  ]);
  
  f_commonQuery('V_STUDENT',
	  function(recs){
		    Ext.each(recs,function(r){
			      var studentScoreStore=addGrid.getStore();
			      var sr=new StudentRecord({
			        'sno':r.get('STUDENT_NO'),
			        'sname':r.get('STUDENT_NAME'),
			        'grade':r.get('GRADE'),
			        'department':r.get('COLLEGE_DEPARTMENT'),
			        'major':r.get('MAJOR'),
			        'departmentName':r.get('COLLEGE_NAME'),
			        'majorName':r.get('MAJOR_NAME')
			      });
			      sr.changeId(r.get('STUDENT_NO'));
			      studentScoreStore.insert(0,sr);
			      addGrid.view.refresh();
		    });	
	  },
	  function(params){}
  );   
}

function f_update(tableName){
  globalScope._update(tableName);
}

function f_delete(tableName){
  globalScope._delete(tableName);
} 

function f_delete2(){
	   var recs=addGrid.getSelectionModel().getSelections();
	   if(recs.length==0){
	     Ext.Msg.alert('WARNING',jspVarHandler.fmt_info_info_required_delete_records);
	     return;	
	   }
   	   for(var i=0;i<recs.length;i++){
   	     var r=recs[i];
   	     addGrid.getStore().remove(r);
   	     addGrid.view.refresh();
   	   }
}

function f_cancel(tableName){
  globalScope._cancel(tableName);
}

function f_save(tableName,url){
  globalScope._save(tableName,url);
}

function f_query(tableName){
    globalScope._query(tableName);
}
function f_gotoPage(pageNo,rowsOfPage,pageCount,rowCount,conditoin){
  globalScope._gotoPage({spanName:"g_pageSpan_<c:out value='${model.tableName}'/>",pageNo:pageNo,rowsOfPage:rowsOfPage,pageCount:pageCount,rowCount:rowCount,condition:conditoin,tableName:"<c:out value='${model.tableName}'/>"});
}
</script>
